package com.five.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.five.exception.BusinessException;
import com.five.jwt.JwtUtils;
import com.five.jwt.Payload;
import com.five.jwt.RsaUtils;
import com.five.pojo.LoginInfo;
import com.five.pojo.Reader;
import com.five.result.Result;
import com.five.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.security.PublicKey;
import java.util.Map;

@RestController
@RequestMapping("/reader/personalCenter")
public class   PersonalCenterController {
    @Autowired
    private ReaderService readerService;


    //修改密码
    @PostMapping("/updatePassword")
    public Result<Map<String,Object>> updatePassword(@RequestHeader("token") String token,  String newPassword) {
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(URLDecoder.decode(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile(), "UTF-8"));
            Payload<Map> payload = JwtUtils.getInfoFromToken(token, publicKey, Map.class);
            Object loginInfo = payload.getUserInfo().get("loginInfo");
            //序列化
            LoginInfo logininfo = JSON.toJavaObject((JSONObject) loginInfo, LoginInfo.class);
            Long readId = logininfo.getId();
            //调用service修改密码 执行update方法
            readerService.updatePassword(newPassword,readId);
            return Result.success("修改密码成功");
        } catch (BusinessException  e){
            return Result.error(e.getMessage());
        }catch (Exception e){
            return Result.error("修改密码失败");
        }
    }
    /*
    *修改个人信息
     */
    @PostMapping("/updateInfo")
    public Result<Map<String,Object>> updateInfo(@RequestHeader("token") String token, @RequestBody Reader reader) {
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(URLDecoder.decode(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile(), "UTF-8"));
            Payload<Map> payload = JwtUtils.getInfoFromToken(token, publicKey, Map.class);
            Object loginInfo = payload.getUserInfo().get("loginInfo");
            //序列化
            LoginInfo logininfo = JSON.toJavaObject((JSONObject) loginInfo, LoginInfo.class);
            Long readId = logininfo.getId();
            Reader reader1 = readerService.queryByLoginInfoId(readId);
            reader.setId(reader1.getId());
            readerService.update(reader);
            return Result.success("修改成功!");
        }catch (BusinessException  e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("修改信息失败");
        }
    }
    /*
    *注销账号
     */
    @PostMapping("/cancelAccount")
    public Result<Map<String,Object>> logout(@RequestHeader("token") String token) {
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(URLDecoder.decode(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile(), "UTF-8"));
            Payload<Map> payload = JwtUtils.getInfoFromToken(token, publicKey, Map.class);
            Object loginInfo = payload.getUserInfo().get("loginInfo");
            //序列化
            LoginInfo logininfo = JSON.toJavaObject((JSONObject) loginInfo, LoginInfo.class);
            Long readId = logininfo.getId();
            readerService.deleteByLoginInfoId(readId);
            return Result.success("注销成功!");
        }catch (BusinessException  e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("注销失败");
        }
    }


}